﻿using Client;
using Shared.Method;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Tools;

namespace YcdTools
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main2()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormAtxPass());
        }
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (CreateMutex() == false)
            {
                MessageBox.Show("该程序正在运行");
                return;
            }


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            MethodWcf.KEY = "RssPmq&AwYcc7<98Bux";

            //inspect the version of product
            //string version = Application.ProductVersion;
            //string name = Application.ProductName;

            //string sqlVersionCheck = $"SELECT CONVERT(VARCHAR,COUNT(*)) FROM SOFTWARE WHERE NAME='{name}' AND VERSION='{version}'";

            //var retVersionCheck = MethodWcf.ExecuteReturnSQLClient(sqlVersionCheck);

            //if (retVersionCheck != "1")
            //{
            //    MessageBox.Show("软件版本不正确，请升级到最新版本");
            //    return;
            //}

            //Application.Run(new Test());

            //return;

            if (int.TryParse(AppSetting.GetValue("FormNum"), out int formNum)
                && int.TryParse(AppSetting.GetValue("LineFormNum"), out int lineFormNum))
            {
                int width = Screen.PrimaryScreen.Bounds.Width;
                int height = Screen.PrimaryScreen.Bounds.Height;

                if (formNum > 16) formNum = 16;
                for (int i = 1; i <= formNum; i++)
                {
                    var point = new System.Drawing.Point
                    {
                        X = (i - 1) % lineFormNum * width / lineFormNum,
                        Y = (i - 1) / lineFormNum * height
                        / (formNum % lineFormNum == 0 ? formNum / lineFormNum : formNum / lineFormNum + 1)
                    };

                    string ip = AppSetting.GetValue("LocalIp" + i);
                    if (int.TryParse(AppSetting.GetValue("LocalPort" + i), out int port) && string.IsNullOrEmpty(ip) == false)
                    {
                        if (i == formNum) Application.Run(new FormCmdTools(ip, port, point));
                        else
                            new FormCmdTools(ip, port, point).Show();
                    }
                    else
                    {
                        MessageBox.Show("本地IP和端口配置错误");
                        Application.Exit();
                    }
                }
            }
            else
            {
                MessageBox.Show("FormNum配置错误");
            }

            ReleaseMutex();//释放独立的进程
        }

        private static Mutex OnlyOne = null;
        public static bool CreateMutex()
        {
            bool result = false;
            try
            {
                Attribute attr = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute));
                string guid = ((GuidAttribute)attr).Value;
                OnlyOne = new Mutex(true, guid, out result);
            }
            catch
            {
                MessageBox.Show("程序启动时发生错误！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return result;
        }

        public static void ReleaseMutex()
        {
            if (OnlyOne != null)
                OnlyOne.Close();
        }
    }
}
